package com.styytech.dadabao.widge.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.styytech.dadabao.R;

/**
 * Created by user on 2015/8/26.
 */
public class PassWordDialog {

    private Dialog mDialog;
   private EditText edt_buy_password;
    private Button btn_buy_ensure,btn_buy_cancel;
    private TextView tv_text;
    private Context mContext;
    private View view;
    private ImageButton ibtn_close;
    StringBuffer text=new StringBuffer();
    String textone="您将支付";
    String texttwo;
    String textthree="元用于投资本项目";
    String text_one="您将提现";
    String text_two;
    String text_three="元";
    /**
     * 判断button的个数*/
    private int type=0;
    Dialogcallback ensurecallback,cancelcallback;

    public PassWordDialog (Context context,String amount){
        this.mContext=context;
        texttwo=amount;
        text.append(textone).append(texttwo).append(textthree);
        initViews();
    }
    public PassWordDialog (Context context,String amount,int type){
        this.mContext=context;
        text_two=amount;
        text.append(text_one).append(text_two).append(text_three);
        initViews();
    }

    private void initViews(){
        mDialog = new Dialog(mContext,
                R.style.ensure_dialog);
        LayoutInflater layoutInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.dialog_password, null);
        tv_text=(TextView)view.findViewById(R.id.tv_text);
        ibtn_close=(ImageButton)view.findViewById(R.id.ibtn_close);
        ibtn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });
        btn_buy_ensure=(Button)view.findViewById(R.id.btn_buy_ensure);
        btn_buy_cancel=(Button)view.findViewById(R.id.btn_buy_cancel);
        edt_buy_password=(EditText)view.findViewById(R.id.edt_buy_password);
        btn_buy_ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ensurecallback.dialogdo();
            }
        });
        btn_buy_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cancelcallback.dialogdo();
            }
        });
        setView();
    }

    /**
     * 设置点击屏幕或者back返回键能否取消*/
    public void NotCancel(boolean cancel){
        mDialog.setCanceledOnTouchOutside(cancel);//false设置点击屏幕其他地方，dialog不消失
        mDialog.setCancelable(cancel);//false设置点击返回键和HOme键，dialog不消失
    }
    public void show(){
        mDialog.show();
    }

    public void dismiss(){
        mDialog.dismiss();
    }

    /**
     * 设定一个interfack接口,使mydialog可以處理activity定義的事情
     * @author sfshine
     *
     */
    public interface Dialogcallback {
        public void dialogdo();
    }
    public void setEnsureDialogCallback(Dialogcallback dialogcallback) {
        this.ensurecallback = dialogcallback;
    }
    public void setCanceleDialogCallback(Dialogcallback dialogcallback) {
        this.cancelcallback = dialogcallback;
    }
    private void setView(){
        //mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        tv_text.setText(text.toString());
        mDialog.setContentView(view);

        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager m = ((Activity)mContext).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        //  p.height = (int) (d.getHeight() * 0.25); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的0.65
        dialogWindow.setAttributes(lp);
    }
    public String getPassword_string(){
        return edt_buy_password.getText().toString();
    }
}

